import request from './request'
// import qs from 'querystring'
const p = process.env.VUE_APP_BASE_URL

// 封装 api 的 try catch 以便复用
const apiFrame = async (type, path, params) => {
  let allData
  try {
    const { data } = await request[type](path, params)
    allData = data
  } catch (e) {
    allData = { success: false, info: '接口错误' }
  }
  return allData
}
/* ***** api ***** */
/*
* 问题：
* 验证码问题
* 时间索引数据不对
*/
export const _Login = params => apiFrame('post', p + '/User/Login', params)
export const _editPw = params => apiFrame('post', p + '/User/AlertPwd', params)
export const _orderList = params => apiFrame('get', p + '/OrderInfo/GetList', { params })
export const _reportList = params => apiFrame('get', p + '/OrderInfo/GetCountList', { params })
export const _orderListA = params => apiFrame('get', p + '/OrderInfo/GetAdminList', { params })
export const _reportListA = params => apiFrame('get', p + '/OrderInfo/GetAdminCountList', { params })
export const _SelectNet = url => apiFrame('get', p + url)
// 从后台下载文件
export const getFile = (url, params = {}, filename = '') => {
  return request({
    url: p + url,
    method: 'get',
    params,
    responseType: 'blob'
  })
    .then(res => {
    /* 兼容ie内核，360浏览器的兼容模式 */
      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        const blob = new Blob([res.data])
        window.navigator.msSaveOrOpenBlob(blob, filename)
      } else {
        /* 火狐谷歌的文件下载方式 */
        var blob = new Blob([res.data])
        var downloadElement = document.createElement('a')
        var href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = filename
        document.body.appendChild(downloadElement)
        downloadElement.click()
        document.body.removeChild(downloadElement)
        window.URL.revokeObjectURL(href)
        return {
          success: true,
          info: '导出成功!'
        }
      }
    })
    .catch(response => {
      console.log(response)
      return {
        success: false,
        info: '导出异常!'
      }
    })
}

// // 客房数据获取
// export const _getHotelList = async (params) => {
//   let allData
//   const path = p + '/Hotel/GetLis'
//   try {
//     const { data } = await request.get(path, { params })
//     allData = data
//   } catch (e) {
//     allData = { success: false, info: '接口错误' }
//   }
//   return allData
// }
